var customerId;

//展示好友邀请
var table1 = YueXiang.Page.getList("/crm/development/invites.html",
		[{
			"name" : "userName",  
			"title" : "用户名" 
		}, {  
			"name" : "",  
			"title" : "注册时间" 
		}, {  
			"name" : "",  
			"title" : "实名认证"
		}, {  
			"name" : "",  
			"title" : "已投资" 
		}, {  
			"name" : "tenderMoney",  
			"title" : "投资金额(元)"
		}, {  
			"name" : "rewardMoney",  
			"title" : "我得到的奖励" 
		}],
		'invite-table',
		function(aoData){
	aoData.push( { "name": "infoId1", "value":$("#invite_serch_cl").html()},{"name": "searchName", "value": $("#inviteSearchName").val()});
},
function(nRow, aData, iDisplayIndex) {  
	/* 执行处理数据 */  
	$('td:eq(1)', nRow).html(new Date(aData.regTime).Format("yyyy-MM-dd hh:mm:ss"));
	if(aData.realNameStatus == 1){
		$('td:eq(2)', nRow).html('<input type="checkbox" disabled="disabled" checked="checked">');
	}else{
		$('td:eq(2)', nRow).html('<input type="checkbox" disabled="disabled">');
	}

	if(aData.isInvested == 1){
		$('td:eq(3)', nRow).html('<input type="checkbox" disabled="disabled" checked="checked">');
	}else{
		$('td:eq(3)', nRow).html('<input type="checkbox" disabled="disabled">');
	}
});
//展示投资记录
var table2 = YueXiang.Page.getList("/crm/invest/records.html",
		[{
			"name" : "id",  
			"title" : "编号" 
		}, {  
			"name" : "borrowName",  
			"title" : "标名" 
		}, {  
			"name" : "timeLimit",  
			"title" : "期数"
		}, {  
			"name" : "money",  
			"title" : "投资金额" 
		}, {  
			"name" : "interest",  
			"title" : "利息"
		},{  
			"name" : "",  
			"title" : "待收金额"
		},  {  
			"name" : "",  
			"title" : "投资时间" 
		}, {  
			"name" : "",  
			"title" : "投资状态" 
		}],
		'invest-table-2',
		function(aoData){
	aoData.push( { "name": "infoId1", "value":$("#invest_serch_cl").html()},{"name": "searchName", "value": $("#investSearchName").val()});
},
function(nRow, aData, iDisplayIndex) {  
	/* 执行处理数据 */
	$('td:eq(5)', nRow).html(aData.money+aData.interest);
	$('td:eq(6)', nRow).html(new Date(aData.addTime).Format("yyyy-MM-dd hh:mm:ss"));
	var s ;
	if (aData.status == 0) {
		s = "投资待处理";
	} else if (aData.status == 1) {
		s = "投资成功";
	} else {
		s = "投资失败";
	}
	$('td:eq(7)', nRow).html(s);
});
//联系信息
var recordTable = YueXiang.Page.getList("/crm/record/list.html",
		[{  
			"name" : "id", 
			"title" : "编号"
		}, {  
			"name" : "addTime",  
			"title" : "日期",
			"width":"100px"
		}, {  
			"name" : "reason",  
			"title" : "沟通计划项",
			"width":"200px"
		}, {  
			"name" : "result",  
			"title" : "执行效果",
			"width":"280px"
		}, {  
			"name" : "action",  
			"title" : "操作"
		}],
		'recordTable',
		function(aoData){
	aoData.push( { "name": "id", "value": customerId} );
},
function(nRow, aData, iDisplayIndex) {  
	$('td:eq(1)', nRow).html(new Date(aData.addTime).Format("yyyy-MM-dd"));
	$('td:eq(3)', nRow).html('<input type="text" style="width:280px;" value="'+aData.result+'" />');
	$('td:eq(4)', nRow).html('<span class="blue" onclick="saveRecord(this)">保存</span>');
});
$(function(){
	//搜索
	$("#investSearchName").change(function(){
		var userId = $("#invest_serch_cl").html();
		if(userId==null||userId==""||typeof(userId)==undefined){
			userId = 0;
		}
		var tender = {
				url : '/crm/invest/' + userId +'.html?searchName='+$(this).val(),
				successCallback : function(response) {
					//alert(response.data);
					var data = response.data;
					if(data!=null&&data!=""&&typeof(data)!=undefined){
						$("#totalTender_cl").html(data.totalTender+"元");
					}else{
						$("#totalTender_cl").html(0+"元");
					}
				},
				errorCallback : function(response){
				}
			};
			YueXiang.Ajax.get(tender);
			
		table2.fnDraw();
	});
	//搜索
	$("#inviteSearchName").change(function(){
		table1.fnDraw();
	});
	
	//每次查看隐藏时清楚数据
	$("#message").on("hidden.bs.modal", function() {
		/** 隐藏表单所有提示内容 */
		$("#message .assign_data").html('');
    });
	
	$("#invite").on("hidden.bs.modal", function() {
		/** 隐藏表单所有提示内容option */
		$("#inviteSearchName").val(0);
    });
	
	$("#invest").on("hidden.bs.modal", function() {
		/** 隐藏表单所有提示内容 */
		$("#investSearchName").val(0);
    });
});


//基本信息
function message(id){
	var message = {
			url : '/crm/development/' + id +'.html',
			successCallback : function(response) {
				var data = response.data.data;
				$("#message #assign_id").html("YX_"+data.id);
				$("#message .assign_source").html(data.source);
				$("#message #assign_mobile").html(data.mobile);
				$("#message .assign_email").html(data.email);
				$("#message .assign_wechat").html(data.wechat);
				$("#message .assign_qq").html(data.qq);
				$("#message .assign_name").html(data.name);
				$("#message .assign_rank").html(data.rank);
				$("#message #assign_desc_").html(data.desc_);
				$("#message .assign_address").html(data.address);
				$("#message .assign_addUser").html(data.addUserName);
				$("#message .assign_addTime").html(new Date(data.addTime).Format("yyyy-MM-dd hh:mm:ss"));
				$("#message .assign_assignUser").html(data.assignUserName);
				if(data.assignTime!=null){
					$("#message .assign_assignTime").html(new Date(data.assignTime).Format("yyyy-MM-dd hh:mm:ss"));
				}else{
					$("#message .assign_assignTime").html('');
				}
				
				
			},
			errorCallback : function(response){
				swal({
					title : "提示信息",
					text : response.msg,
					type : "error"
				},function(){
					$('#assign').modal('hide');
					table.fnDraw();
					return;
				});
				return;
			}
	};
	YueXiang.Ajax.get(message);
}
//联系记录
function showRecord(id){
	customerId = id;
	recordTable.fnDraw();
}

//平台信息展示
function establish(id){
	var establish = {
			url : '/crm/development/' + id +'.html',
			successCallback : function(response) {
				var data = response.data.data;
				$("#establish_modal .establish_mobile").html(data.mobile);
				$("#establish_modal .establish_name").html(data.name);
				$("#establish_modal .establish_cardId").html(data.cardId);
				if(data.tppStatus == 1){
					$("#establish_modal .establish_isReg").html('<input type="checkbox" disabled="disabled" checked="checked">');
				}else{
					$("#establish_modal .establish_isReg").html('<input type="checkbox" disabled="disabled">');
				}

				if(data.isTender == 1){
					$("#establish_modal .establish_isTender").html('<input type="checkbox" disabled="disabled" checked="checked">');
				}else{
					$("#establish_modal .establish_isTender").html('<input type="checkbox" disabled="disabled">');
				}
				if(data.loginTime!=null&&data.loginTime!=''&&typeof(data.loginTime)!=undefined){
					$("#establish_modal .establish_loginTime").html(new Date(data.loginTime).Format("yyyy-MM-dd hh:mm:ss"));
				}
				if(data.regTime!=null&&data.regTime!=''&&typeof(data.regTime)!=undefined){
					$("#establish_modal .establish_regTime").html(new Date(data.regTime).Format("yyyy-MM-dd hh:mm:ss"));
				}

			},
			errorCallback : function(response){
				swal({
					title : "提示信息",
					text : response.msg,
					type : "error"
				},function(){
					$('#assign').modal('hide');
					table.fnDraw();
					return;
				});
				return;
			}
	};
	YueXiang.Ajax.get(establish);
}

/**
 * 好友邀请详情
 */
function invite(id,userId){
	$("#invite_serch_cl").html(id);
	//获取对应的：当前用户的所有佣金与所有的已注册人数与投资人数
	if(userId==null||userId==""||typeof(userId)==undefined){
		userId = 0;
	}
	var agentModle = {
			url : '/crm/agentModel/' + userId +'.html',
			successCallback : function(response) {
				//alert(response.data);
				var data = response.data;
				if(data!=null&&data!=""&&typeof(data)!=undefined){
					$("#rewardMoney").html(data.rewardMoney+"元");
					$("#inviteCount").html(data.inviteCount+"人");
					$("#tenderPeople").html(data.tenderCount+"人");
				}else{
					$("#rewardMoney").html(0+"元");
					$("#inviteCount").html(0+"人");
					$("#tenderPeople").html(0+"人");
				}
			},
			errorCallback : function(response){
			}
	};
	YueXiang.Ajax.get(agentModle);

	table1.fnDraw();
}
//投资记录
function invest(userId){
	$("#invest_serch_cl").html(userId);
	//获取对应的：当前用户的所有佣金与所有的已注册人数与投资人数
	if(userId==null||userId==""||typeof(userId)==undefined){
		userId = 0;
	}
	var tender = {
			url : '/crm/invest/' + userId +'.html?searchName=0',
			successCallback : function(response) {
				//alert(response.data);
				var data = response.data;
				if(data!=null&&data!=""&&typeof(data)!=undefined){
					$("#totalTender_cl").html(data.totalTender+"元");
				}else{
					$("#totalTender_cl").html(0+"元");
				}
			},
			errorCallback : function(response){
			}
	};
	YueXiang.Ajax.get(tender);
	table2.fnDraw();
}
//联系记录
//添加详细记录列
function addRecord(){
	var html = '<tr><td></td><td><input type="text" onFocus="WdatePicker({dateFmt:\'yyyy-MM-dd\',readOnly:true})" class="searchTime" style="width:100px;"/></td>'
		+ '<td><input type="text"  /></td><td><input type="text" style="width:280px;" /></td><td class="cursor"><span class="blue" onclick="saveRecord(this);">保存</span><span class="blue">&nbsp;|&nbsp;</span><span class="blue" onclick="deleteRecord(this)">删除</span></td></tr>';
	$("#recordTable tbody").append(html);
}
//删除详细记录列
function deleteRecord(elem){
	var $tr = $(elem).closest("tr");
	var $tds = $tr.find("td");
	var id = $tds.eq(0).text();
	if(id != '' && parseInt(id) > 0){
		
	} else {
		$tr.remove();
	} 
}
//保存详细记录
function saveRecord(elem){
	var $tr = $(elem).closest("tr");
	var $tds = $tr.find("td");
	var id = $tds.eq(0).text();
	if(id == '' || parseInt(id) <= 0){
		id = 0 ;
	}
	var addTime = $tds.eq(1).find("input").val();
	var reason = $tds.eq(2).find("input").val();
	var result = $tds.eq(3).find("input").val();
	var addSave = {
			url:"/crm/record.html",
			formData : {
				id:id,
				addTime : addTime,
				reason : reason,
				result : result,
				customerId : customerId
			},
			successCallback : function(response) {
				if(response.data>0){
					recordTable.fnDraw();
					YueXiang.Popup.success(response.msg);
				}else{
					YueXiang.Popup.error(response.msg);
				}
			},
			errorCallback : function(response){
				YueXiang.Popup.error(response.msg);
			}
	};
	YueXiang.Ajax.postNoForm(addSave);
}
//查看汇付账户
function queryUserBalance(id){
	var getMessage = {
			url : '/crm/userBalance/' + id +'.html',
			successCallback : function(response) {
				var data = response.data;
				$("#totalMoney").html(data.totalMoney);
				$("#userMoney").html(data.userMoney);
				$("#noMoney").html(data.noMoney);
			},
			errorCallback : function(response){
				YueXiang.Popup.error(response.msg);
			}
	};
	YueXiang.Ajax.get(getMessage);
}